import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

/**
 * 
 * User's Login Window in to the System
 *
 */


public class LoginWindow implements ActionListener
{
	/** used any time the server is called, for busy-wait synchronization */
	public Boolean flag=false;
/** used any time the server is called for the Count Reviews Thread only, for busy-wait synchronization */
	public Boolean flag2=false;
	/** log in to the System button */
	JButton okButton;
	/** exit the program */
	JButton exitButton;
	/** frame for the window */
	public JFrame frame;
	/** frame for the Configuration window */
	public JFrame conf=null;
	/** user's password */
	JTextField pass;
	/** user's username */
	JTextField user;
	/** server ip number */
	JTextField ipNum;
	/** window panel */
	public JPanel panel;
	/** holds login data */
	public String[] loginData;
	/** instance of client query */
	public ClientQuery connection;
	/** database configuration button */
	private JButton config; 

	/** initializes the window's Gui and variables adds listeners to buttons */
	public LoginWindow()
	{
			
		frame = new JFrame("Good Reading"); 
		frame.setPreferredSize(new Dimension(650,550));
		final JLabel welcomeLabel = new JLabel("WELCOME");
		welcomeLabel.setFont(new Font(Font.SANS_SERIF ,Font.ITALIC,40));
		welcomeLabel.setBounds(200,20,300,50);
		
		
		okButton=new JButton("Login");
		okButton.setBounds(450, 400, 100, 50);
		okButton.addActionListener(this); 
	

		config=new JButton("Configuration");
		config.setBounds(250, 400, 150, 50);
		config.addActionListener(this); 
		
		
		exitButton=new JButton("Exit");
		exitButton.setBounds(100,400,100,50);
		exitButton.addActionListener(this);
		
	
		ipNum=new JTextField();
		ipNum.setBounds(200,100,300,50);
		ipNum.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,25));
		
		user=new JTextField();
		user.setBounds(200,200,300,50);
		user.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,25));
		
		pass=new JPasswordField();
		pass.setBounds(200,300,300,50);
		pass.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,25));
		
		final JLabel ipLabel = new JLabel("IP :");
		ipLabel.setFont(new Font(Font.SANS_SERIF ,Font.BOLD,25));
		ipLabel.setBounds(50,100,100,50);
		
		final JLabel userLabel = new JLabel("Username :");
		userLabel.setFont(new Font(Font.SANS_SERIF ,Font.BOLD,25));
		userLabel.setBounds(50,200,150,50);
		
		final JLabel passLabel = new JLabel("Password :");
		passLabel.setFont(new Font(Font.SANS_SERIF ,Font.BOLD,25));
		passLabel.setBounds(50,300,150,50);
		
		
		panel = new JPanel();
		panel.setLayout(null);
		panel.add(welcomeLabel);
		panel.add(ipLabel);
		panel.add(userLabel);
		panel.add(passLabel);
		panel.add(okButton);
		panel.add(exitButton);
		panel.add(user);
		panel.add(pass);
		panel.add(ipNum);
		panel.add(config);
	
		
		
		
		
		
		panel.setEnabled(true);
		panel.setVisible(true);
		frame.getContentPane().add(panel);
		
       	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		frame.pack();
		frame.setVisible(true);
		
	}
	
	/** if one the buttons is pressed this method is called */
	
	public void actionPerformed(java.awt.event.ActionEvent e)
	{
		if(e.getSource()==okButton)
		{
			loginData=new String[8];
			loginData[0]=Main.myIP;
			loginData[1]="login";
			loginData[2]=user.getText();
			loginData[3]=pass.getText();
			loginData[4]=Main.sets[0];
			loginData[5]=Main.sets[1];
			loginData[6]=Main.sets[2];
			loginData[7]=Main.sets[3];
			Main.serverIP=ipNum.getText();
			if (Main.serverIP == null)
				Main.serverIP="localhost";
	    	if(loginData[2].equals("")||loginData[3].equals(""))
	    	{
	    		 JOptionPane.showMessageDialog(null,"One or more fields are empty, Please try again !","Login Eror",JOptionPane.ERROR_MESSAGE);
	    		 return;
	    	 }
	    	else connection= new ClientQuery(Main.serverIP,5555); 
			if(connection.client!=null)
			{
				connection.accept(loginData[1]);
				String res=ChatClient.getRs();
				while (res==null) {
					connection.accept(loginData[1]);
					res=ChatClient.getRs();
				}
				String[] result=res.split("\\*");
				if(result[0].equals("OK"))
			    {
					Main.gusers=new User(result);
					Main.log.panel.setVisible(false);
					Main.log.panel.setEnabled(false);
					Main.welcomeScreen= new MainScreen();				
					if(Main.gusers.authorozations==2 || Main.gusers.authorozations==3)
						Main.welcomeScreen.SetReviewsCount();
					if(Main.gusers.authorozations==1)
					{
						Main.size[0]=450;
						Main.size[1]=320;
					}
					else if(Main.gusers.authorozations==2)
					{
						Main.size[0]=450;
						Main.size[1]=400;
					}
					else if(Main.gusers.authorozations==3)
					{
						Main.size[0]=450;
						Main.size[1]=515;
					}
					Main.log.frame.setSize(Main.size[0],Main.size[1]);
					Main.log.frame.getContentPane().add(Main.welcomeScreen.panel);
					Main.welcomeScreen.panel.setVisible(true);
					Main.welcomeScreen.panel.setEnabled(true);
			    }
				else if(result[0].equals("IN"))
				{
					//System.out.printf("already in");
					loginData=new String[4];
					loginData[0]=Main.myIP;
					loginData[1]="logout";
					loginData[2]=user.getText();
					loginData[3]="logedIn";
					Main.log.connection.accept("logout");
					JOptionPane.showMessageDialog(null,"You Already Logged In","ERROR",JOptionPane.ERROR_MESSAGE);
				}
				else if(result[0].equals("WRONG")) 
				{
					loginData=new String[4];
					loginData[0]=Main.myIP;
					loginData[1]="logout";
					loginData[2]=user.getText();
					loginData[3]=pass.getText();
					connection.accept("logout");
					JOptionPane.showMessageDialog(null,"Wrong Username or Password","ERROR",JOptionPane.ERROR_MESSAGE);
				}
				else if(result[0].equals("FROZEN")) 
				{
					loginData=new String[4];
					loginData[0]=Main.myIP;
					loginData[1]="logout";
					loginData[2]=user.getText();
					loginData[3]=pass.getText();
					connection.accept("logout");
					JOptionPane.showMessageDialog(null,"Your Account Was Frozen, Contact Administrator","ERROR",JOptionPane.ERROR_MESSAGE);
				}
				else JOptionPane.showMessageDialog(null,result[0],"ERROR",JOptionPane.ERROR_MESSAGE);
				
				
			}
			
		}

	    
		if(e.getSource()==exitButton)
		{
			System.exit(0);
		
		}
		if(e.getSource()==config)
		{
			new Configuration(conf);
			panel.setEnabled(false);
		}
	}
}

